package cn.itcast.bos.web.action.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.service.Customer;
import cn.itcast.crm.service.CustomerService;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
@Namespace("/")//<package namespace extends
@ParentPackage("struts-default")
@Results({@Result(name="list",type="redirect",location="/pages/base/standard.jsp")})
public class StandardAction extends BaseAction<Standard> {
	
	/*private Standard model=new Standard();
	public Standard getModel() {
		return model;
	}*/
	
	@Autowired
	private StandardService standardService;
	
	//<action name="" class="" method=">
	/**
	 * @Description:通过模型驱动接收收派标准相关数据，实现收派标准保存
	 */
	@Action("standarAction_save")
	public String save() throws Exception {
		standardService.save(model);
		return "list";
	}
	
	
	
	/**
	 * @Description:通过模型驱动接收收派标准相关数据(获取id)，实现收派标准删除
	 */
	@Action("standarAction_delete")
	public String delete() throws Exception {
		standardService.delete(model.getId());
		return "list";
	}
	
	
	
	
	/*//通过属性驱动接收页面提交当前页，每页显示记录数
	private int rows;
	private int page;
	public void setRows(int rows) {
		this.rows = rows;
	}
	public void setPage(int page) {
		this.page = page;
	}*/
	
	/*@Autowired
	private CustomerService customerProxy;*/
	
	

	/**
	 *  @Description: datagrid提交ajax请求；收派标准列表分页查询
	 *  @return  json对象
	 *  {
	  	"total":100,
	  	"rows":[{},{}]
	  	}
	 */
	@Action("standardAction_pageQuery")
	public String pageQuery() throws Exception {
		
		/*//TODO 测试远程调用CRM查询所有客户的方法
		List<Customer> list = customerProxy.findAll();
		for (Customer customer : list) {
			System.out.println(customer.getUsername());
		}*/
		
		
		//调用dao中findAll方法： Page<T> findAll(Pageable pageable);
		//Pageable对象中封装当前页，每页显示记录数
		Pageable pageable=new PageRequest(page-1,rows);
		//Page对象中封装总记录数，当前页记录等
		Page<Standard> page=standardService.pageQuery(pageable);
		System.out.println("总记录数："+page.getTotalElements());
		System.out.println("当前页记录："+page.getContent());
		this.java2Json(page,null);
		
		
		/*//将java对象转为json
		//常见转json工具包：json-lib ，fastjson， gson，jackson
		//数组类型json格式 JsonArray.fromObject(数组或者list集合)
		//对象形式的json格式 JsonObject.fromObject(javaBean或者map集合)
		Map<String, Object> map=new HashMap<>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());
		String json=JSONObject.fromObject(map).toString();
		System.err.println(json);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(json);*/
		return NONE;
	}
	
	/**
	 * @Description:ajax请求,查询所有收派记录                                                                                                                                       
	 * @return  json数组
	 * @throws Exception
	 */
	@Action("standardAction_findAll")
	public String findAll() throws Exception {
		List<Standard> list=standardService.findAll();
		this.java2Json(list, null);
		/*String json=JSONArray.fromObject(list).toString();
		System.err.println(json);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(json);*/
		return NONE;
	}

}
